﻿using System.Security.Cryptography;
using System.Text;

namespace ECommon
{
    /// <summary>
    /// 哈希工具
    /// </summary>
    public static partial class HashTool
    {
        /// <summary>
        /// 取utf8编码字符串的哈希值
        /// </summary>
        /// <param name="utf8String">utf8编码的字符串</param>
        /// <returns>哈希值</returns>
        public static string GetHash(string utf8String)
        {
            if (string.IsNullOrEmpty(utf8String))
            {
                return string.Empty;
            }
            using (SHA256 sha256Hash = SHA256.Create())
            {
                // 将字符串转换为字节数组
                byte[] data = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(utf8String));

                return StringTool.GetStringFromByteArray(data);
            }
        }
    }
}
